<?php echo "hello world"; exit;
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo "start<br>";

session_start();

// 删除旧文件
if (isset($_SESSION['db_file']) && file_exists($_SESSION['db_file'])) {
    unlink($_SESSION['db_file']);
    unset($_SESSION['db_file']);
}

// 上传调试
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "POST received<br>";
    echo '<pre>$_FILES: ' . print_r($_FILES, true) . '</pre>';
    echo '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
    if (isset($_FILES['dbfile'])) {
        echo "dbfile字段存在<br>";
        $tmp_name = $_FILES['dbfile']['tmp_name'];
        $ext = pathinfo($_FILES['dbfile']['name'], PATHINFO_EXTENSION);
        $uploads_dir = __DIR__ . '/uploads';
        if (!is_dir($uploads_dir)) {
            mkdir($uploads_dir, 0777, true);
        }
        $db_file = $uploads_dir . '/sqlite_' . uniqid() . '.' . $ext;
        echo "准备上传，临时文件: $tmp_name, 目标: $db_file<br>";
        echo "uploads文件夹可写: " . (is_writable($uploads_dir) ? '是' : '否') . "<br>";
        echo "upload_max_filesize: " . ini_get('upload_max_filesize') . "<br>";
        echo "post_max_size: " . ini_get('post_max_size') . "<br>";
        if (move_uploaded_file($tmp_name, $db_file)) {
            echo "上传成功<br>";
            $_SESSION['db_file'] = $db_file;
            header("Location: ?show=1");
            exit;
        } else {
            echo "上传失败<br>";
            echo '<pre>' . print_r($_FILES, true) . '</pre>';
            exit;
        }
    } else {
        echo "dbfile字段不存在<br>";
    }
    exit;
}

// 删除文件请求
if (isset($_GET['delete']) && isset($_SESSION['db_file'])) {
    if (file_exists($_SESSION['db_file'])) unlink($_SESSION['db_file']);
    unset($_SESSION['db_file']);
    exit('ok');
}

// 展示数据库内容
if (isset($_GET['show']) && isset($_SESSION['db_file'])) {
    $db_file = $_SESSION['db_file'];
    echo "show模式<br>";
    echo "db_file: $db_file<br>";
    echo "文件是否存在: " . (file_exists($db_file) ? '是' : '否') . "<br>";
    if (file_exists($db_file)) {
        echo "文件大小: " . filesize($db_file) . " 字节<br>";
    }
    // 继续原有表格展示逻辑
    if (!file_exists($db_file)) {
        echo "<p style='color:red'>数据库文件不存在！</p>";
        exit;
    }
    $db = new SQLite3($db_file);
    $tables = [];
    $res = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");
    while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
        $tables[] = $row['name'];
    }
    echo "表数量: ".count($tables)."<br>";
    if (empty($tables)) {
        echo "<p style='color:red'>数据库中没有表，或不是有效的SQLite数据库！</p>";
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>SQLite数据库查看器</title>
        <style>
            body { font-family: Arial; margin: 20px; }
            .tabs { display: flex; border-bottom: 2px solid #ccc; }
            .tab { padding: 10px 20px; cursor: pointer; border: 1px solid #ccc; border-bottom: none; background: #f9f9f9; }
            .tab.active { background: #fff; font-weight: bold; }
            .table-content { display: none; }
            .table-content.active { display: block; }
            table { border-collapse: collapse; width: 100%; margin-top: 10px; }
            th, td { border: 1px solid #ccc; padding: 6px 10px; }
            th { background: #f0f0f0; }
            tr:nth-child(even) { background: #f9f9f9; }
        </style>
    </head>
    <body>
        <h2>SQLite数据库查看器</h2>
        <div class="tabs">
            <?php foreach ($tables as $i => $table): ?>
                <div class="tab<?= $i==0?' active':'' ?>" data-tab="<?= $i ?>"><?= htmlspecialchars($table) ?></div>
            <?php endforeach; ?>
        </div>
        <?php foreach ($tables as $i => $table): ?>
            <div class="table-content<?= $i==0?' active':'' ?>" id="tab-<?= $i ?>">
                <h3><?= htmlspecialchars($table) ?></h3>
                <table>
                    <tr>
                        <?php
                        $res = $db->query("PRAGMA table_info('$table')");
                        $cols = [];
                        while ($col = $res->fetchArray(SQLITE3_ASSOC)) {
                            $cols[] = $col['name'];
                            echo '<th>' . htmlspecialchars($col['name']) . '</th>';
                        }
                        ?>
                    </tr>
                    <?php
                    $res = $db->query("SELECT * FROM '$table' LIMIT 1000");
                    while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
                        echo '<tr>';
                        foreach ($cols as $col) {
                            echo '<td>' . htmlspecialchars($row[$col]) . '</td>';
                        }
                        echo '</tr>';
                    }
                    ?>
                </table>
            </div>
        <?php endforeach; ?>
        <script>
            // 标签切换
            document.querySelectorAll('.tab').forEach(function(tab, idx){
                tab.onclick = function(){
                    document.querySelectorAll('.tab').forEach(t=>t.classList.remove('active'));
                    document.querySelectorAll('.table-content').forEach(c=>c.classList.remove('active'));
                    tab.classList.add('active');
                    document.getElementById('tab-'+idx).classList.add('active');
                }
            });
            // 页面关闭/刷新时删除数据库
            window.addEventListener('beforeunload', function(){
                navigator.sendBeacon('?delete=1');
            });
        </script>
    </body>
    </html>
    <?php
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>SQLite数据库查看器</title>
    <style>
        body { font-family: Arial; margin: 20px; }
        .upload-box { border: 1px solid #ccc; padding: 30px; width: 350px; margin: 100px auto; background: #f9f9f9; }
    </style>
</head>
<body>
    <div class="upload-box">
        <h2>上传SQLite数据库文件</h2>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="dbfile" accept=".db,.sqlite,.sqlite3" required>
            <br><br>
            <button type="submit">上传并查看</button>
        </form>
    </div>
</body>
</html> 